﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Client;
using System.Net;
using System.Xml;
using System.Text.RegularExpressions;
using WapCrawlerCore;
using WapCrawlerCore.Classes;
using WapCrawlerLib;


namespace WapCrawlerCore
{
    public class BloggerAPI
    {
        /** Lists the user's blogs. */
        public void ListUserBlogs(Service service)
        {
            Console.WriteLine("\nRetrieving a list of blogs");
            FeedQuery query = new FeedQuery();
            // Retrieving a list of blogs
            query.Uri = new Uri("http://www.blogger.com/feeds/default/blogs");
            AtomFeed feed = null;
            feed = service.Query(query);
            foreach (AtomEntry entry in feed.Entries)
            {
                Console.WriteLine("  Blog title: " + entry.Title.Text);
            }
        }

        ///** Lists the user's blogs and returns the URI for posting new entries
        // * to the blog which the user selected.
        // */
        //public Uri SelectUserBlog(Service service)
        //{
        //    //Console.WriteLine("\nPlease select a blog on which to post.");
        //    FeedQuery query = new FeedQuery();
        //    // Retrieving a list of blogs
        //    query.Uri = new Uri("http://www.blogger.com/feeds/default/blogs");
        //    AtomFeed feed = service.Query(query);

        //    // Publishing a blog post
        //    Uri blogPostUri = null;
        //    if (feed != null)
        //    {
        //        AtomEntry entry = feed.Entries[0];
        //        // Print out the title of the Blog
        //        //Console.WriteLine("  Blog name: " + entry.Title.Text);
        //        //Console.Write("  Post to this blog? (y/n): ");
        //        //if (Console.ReadLine().Equals("y"))
        //        //{
        //        // find the href in the link with a rel pointing to the blog's feed
        //        for (int i = 0; i < entry.Links.Count; i++)
        //        {
        //            if (entry.Links[i].Rel.Equals("http://schemas.google.com/g/2005#post"))
        //            {
        //                blogPostUri = new Uri(entry.Links[i].HRef.ToString());
        //                //Console.WriteLine("  Your new posts will be sent to " + blogPostUri.AbsoluteUri.ToString());
        //            }
        //        }
        //        //return blogPostUri;
        //        //}

        //    }
        //    return blogPostUri;
        //}

        /** Lists the user's blogs and returns the URI for posting new entries
         * to the blog which the user selected.
         */
        public Uri SelectUserBlog(Service service)
        {
            //Console.WriteLine("\nPlease select a blog on which to post.");
            FeedQuery query = new FeedQuery();
            // Retrieving a list of blogs
            query.Uri = new Uri("http://www.blogger.com/feeds/default/blogs");
            AtomFeed feed = service.Query(query);

            // Publishing a blog post
            Uri blogPostUri = null;
            if (feed != null)
            {
                AtomEntry entry = feed.Entries[0];

                // Print out the title of the Blog
                Console.WriteLine("  Blog name: " + entry.Title.Text);
                //Console.Write("  Post to this blog? (y/n): ");
                //if (Console.ReadLine().Equals("y"))
                //{
                // find the href in the link with a rel pointing to the blog's feed
                for (int i = 0; i < entry.Links.Count; i++)
                {
                    if (entry.Links[i].Rel.Equals("http://schemas.google.com/g/2005#post"))
                    {
                        blogPostUri = new Uri(entry.Links[i].HRef.ToString());
                        //Console.WriteLine("  Your new posts will be sent to " + blogPostUri.AbsoluteUri.ToString());
                    }
                }
                //return blogPostUri;
                //}

            }
            return blogPostUri;
        }


        /** Creates a new blog entry and sends it to the specified Uri */
        public AtomEntry PostNewEntry(Service service, Uri blogPostUri, PostCrawler objPostCrawler, string topic)
        {
            AtomEntry createdEntry = null;
            if (blogPostUri != null)
            {
                // construct the new entry
                AtomEntry newPost = new AtomEntry();
                //Tạo title không dấu trước để tạo url seo
                if (objPostCrawler.postSeoTitle != null && objPostCrawler.postSeoTitle != "")
                    newPost.Title.Text = objPostCrawler.postSeoTitle;
                else
                {
                    Seo objSeo = new Seo();
                    newPost.Title.Text = objSeo.ConvertTitle(objPostCrawler.postTitle);
                }
                //get labels
                //TopicPostCrawler objTopicPost = new TopicPostCrawler();
                
                //foreach (TopicPostCrawler objTopicPost in objPostCrawler.TopicPostCrawlers)
                //{
                    AtomCategory category = new AtomCategory();
                    category.Scheme = "http://www.blogger.com/atom/ns#";
                    category.Term = topic;
                    newPost.Categories.Add(category);
                //}
                newPost.Content = new AtomContent();
                newPost.Content.Content = objPostCrawler.postContent;
                newPost.Content.Type = "html";
                createdEntry = service.Insert(blogPostUri, newPost);
                //Update title again
                createdEntry.Title.Text = objPostCrawler.postTitle;
                createdEntry = createdEntry.Update();
                if (createdEntry != null)
                {
                    Console.WriteLine("  New blog post created with title: " + createdEntry.Title.Text);
                }
            }
            return createdEntry;
        }

        /** Creates a new blog entry and sends it to the specified Uri */
        public AtomEntry PostNewEntry(Service service, Uri blogPostUri, Post objPost, string topic)
        {
            AtomEntry createdEntry = null;
            if (blogPostUri != null)
            {
                // construct the new entry
                AtomEntry newPost = new AtomEntry();
                //Tạo title không dấu trước để tạo url seo
                if (objPost.seoTitle != null && objPost.title != "")
                    newPost.Title.Text = objPost.seoTitle;
                else
                {
                    Seo objSeo = new Seo();
                    newPost.Title.Text = objSeo.ConvertTitle(objPost.title);
                }
                //get labels
                //TopicPostCrawler objTopicPost = new TopicPostCrawler();

                //foreach (TopicPostCrawler objTopicPost in objPost.TopicPostCrawlers)
                //{
                AtomCategory category = new AtomCategory();
                category.Scheme = "http://www.blogger.com/atom/ns#";
                category.Term = topic;
                newPost.Categories.Add(category);
                //}
                newPost.Content = new AtomContent();
                newPost.Content.Content = objPost.content;
                newPost.Content.Type = "html";
                createdEntry = service.Insert(blogPostUri, newPost);
                //Update title again
                createdEntry.Title.Text = objPost.title;
                createdEntry = createdEntry.Update();
                if (createdEntry != null)
                {
                    Console.WriteLine("  New blog post created with title: " + createdEntry.Title.Text);
                }
            }
            return createdEntry;
        }

        ///** Creates a new blog entry and sends it to the specified Uri */
        //public AtomEntry PostNewEntry(Service service, Uri blogPostUri)
        //{
        //    //Console.WriteLine("\nPublishing a blog post");
        //    //AtomCategory tag = new AtomCategory("Truyen", "http://www.blogger.com/atom/ns#");
        //    //tag.Label = "Truyen";
        //    AtomEntry createdEntry = null;
        //    if (blogPostUri != null)
        //    {
        //        // construct the new entry
        //        AtomEntry newPost = new AtomEntry();
        //        newPost.Title.Text = "Marriage!";
        //        //newPost.Categories.Add(tag);
        //        newPost.Content = new AtomContent();
        //        newPost.Content.Content = "<div xmlns=\"http://www.w3.org/1999/xhtml\">" +
        //            "<p>Mr. Darcy has <em>proposed marriage</em> to me!</p>" +
        //            "<p>He is the last man on earth I would ever desire to marry.</p>" +
        //            "<p>Whatever shall I do?</p>" +
        //            "</div>";
        //        newPost.Content.Type = "xhtml";
        //        newPost.Authors.Add(new AtomPerson());
        //        newPost.Authors[0].Name = "Elizabeth Bennet";
        //        newPost.Authors[0].Email = "liz@gmail.com";

        //        createdEntry = service.Insert(blogPostUri, newPost);
        //        if (createdEntry != null)
        //        {
        //            Console.WriteLine("  New blog post created with title: " + createdEntry.Title.Text);
        //        }
        //    }
        //    return createdEntry;
        //}

        /** Creates a new blog entry and sends it to the specified Uri */
        public void PostAndDeleteNewDraftEntry(Service service, Uri blogPostUri)
        {
            Console.WriteLine("\nCreating a draft blog post");
            AtomEntry draftEntry = null;
            if (blogPostUri != null)
            {
                // construct the new entry
                AtomEntry newPost = new AtomEntry();
                newPost.Title.Text = "Marriage! (Draft)";
                newPost.Content = new AtomContent();
                newPost.Content.Content = "<div xmlns=\"http://www.w3.org/1999/xhtml\">" +
                    "<p>Mr. Darcy has <em>proposed marriage</em> to me!</p>" +
                    "<p>He is the last man on earth I would ever desire to marry.</p>" +
                    "<p>Whatever shall I do?</p>" +
                    "</div>";
                newPost.Content.Type = "xhtml";
                newPost.Authors.Add(new AtomPerson());
                newPost.Authors[0].Name = "Elizabeth Bennet";
                newPost.Authors[0].Email = "liz@gmail.com";
                newPost.IsDraft = true;

                draftEntry = service.Insert(blogPostUri, newPost);
                if (draftEntry != null)
                {
                    Console.WriteLine("  New draft post created with title: " + draftEntry.Title.Text);
                    // Delete the newly created draft entry
                    Console.WriteLine("  Press enter to delete the draft blog post");
                    Console.ReadLine();
                    draftEntry.Delete();
                }
            }
        }

        /** Display the titles for all entries in the previously selected blog. */
        public SortedDictionary<string, AtomEntry> ListBlogEntries(Service service, Uri blogUri, string label)
        {
            SortedDictionary<string, AtomEntry> listEntries = new SortedDictionary<string, AtomEntry>();
            if (blogUri != null)
            {
                // Retrieve all posts in a blog
                FeedQuery query = new FeedQuery();
                query.Uri = blogUri;
                
                //Lọc theo label
                if (label != ""){
                AtomCategory atomCat = new AtomCategory(label);
                QueryCategory queryCat = new QueryCategory(atomCat);
                query.Categories.Add(queryCat);
                }

                //Lấy từ ngày đầu post
                //query.MinPublication = new DateTime(2012, 1, 1);
                query.MaxPublication = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);

                AtomFeed feed = service.Query(query);
                foreach (AtomEntry entry in feed.Entries)
                {
                    if (listEntries.ContainsKey(entry.Title.Text))
                        DeleteEntry(entry);
                    else
                        listEntries.Add(entry.Title.Text, entry);
                }
            }
            return listEntries;
        }

        ///** Display title for entries in the blog in the hard coded date range. */
        //public void ListBlogEntriesInDateRange(Service service, Uri blogUri)
        //{
        //    Console.WriteLine("\nRetrieving posts using query parameters");
        //    // Retrieve all posts in a blog between Jan 1, 2006 and Apr 12, 2007
        //    FeedQuery query = new FeedQuery();
        //    query.Uri = blogUri;
        //    query.MinPublication = new DateTime(2006, 1, 1);
        //    query.MaxPublication = new DateTime(2007, 4, 12);
        //    AtomFeed feed = service.Query(query);
        //    foreach (AtomEntry entry in feed.Entries)
        //    {
        //        Console.WriteLine("  Entry Title: " + entry.Title.Text);
        //    }
        //}

        /// <summary>
        /// Change the contents of the newly created blog entry
        /// </summary>
        /// <param name="toEdit"></param>
        /// <param name="objPostCrawler"></param>
        /// <param name="topic"></param>
        /// <returns></returns>
        public AtomEntry EditEntry(AtomEntry toEdit, PostCrawler objPostCrawler, string topic)
        {
            // Edit the new entry
            if (toEdit != null)
            {
                //Xóa toàn bộ label hiện có 
                toEdit.Categories.Clear();

                //Update lại label
                AtomCategory category = new AtomCategory();
                category.Scheme = "http://www.blogger.com/atom/ns#";
                category.Term = topic;
                if (!toEdit.Categories.Contains(category))
                    toEdit.Categories.Add(category);

                //Cập nhật nội dung
                toEdit.Title.Text = objPostCrawler.postTitle;
                toEdit.Content.Content = objPostCrawler.postContent;
                toEdit.Content.Type = "html";
                toEdit = toEdit.Update();
            }
            return toEdit;
        }

        /// <summary>
        /// Change the contents of the newly created blog entry
        /// </summary>
        /// <param name="toEdit"></param>
        /// <param name="objPostCrawler"></param>
        /// <param name="topic"></param>
        /// <returns></returns>
        public AtomEntry EditEntry(AtomEntry toEdit, Post objPost, string topic)
        {
            // Edit the new entry
            if (toEdit != null)
            {
                //Xóa toàn bộ label hiện có 
                toEdit.Categories.Clear();

                //Update lại label
                AtomCategory category = new AtomCategory();
                category.Scheme = "http://www.blogger.com/atom/ns#";
                category.Term = topic;
                if (!toEdit.Categories.Contains(category))
                    toEdit.Categories.Add(category);

                //Cập nhật nội dung
                toEdit.Title.Text = objPost.title;
                toEdit.Content.Content = objPost.content;
                toEdit.Content.Type = "html";
                toEdit = toEdit.Update();
            }
            return toEdit;
        }

        /** Delete the specified blog entry. */
        public void DeleteEntry(AtomEntry toDelete)
        {
            Console.WriteLine("\nDeleting post");
            // Delete the edited entry
            if (toDelete != null)
            {
                Console.WriteLine("  Press enter to delete the new blog post");
                Console.ReadLine();
                toDelete.Delete();
            }
        }

        /** Get the comments feed URI for the desired blog entry. */
        public Uri SelectBlogEntry(Service service, Uri blogPostUri)
        {
            Console.WriteLine("\nPlease select a blog entry on which to comment.");
            FeedQuery query = new FeedQuery();
            query.Uri = blogPostUri;
            AtomFeed feed = service.Query(query);
            Uri commentPostUri = null;

            if (feed != null)
            {
                foreach (AtomEntry entry in feed.Entries)
                {
                    // Print out the title of the Blog
                    Console.WriteLine("  Blog entry title: " + entry.Title.Text);
                    Console.Write("  Post a comment on this entry? (y/n): ");

                    if (Console.ReadLine().Equals("y"))
                    {
                        // Create the Post URL for adding a comment by finding this entry's id number.

                        // Find the href in the link with a rel pointing to the blog's feed.
                        for (int i = 0; i < entry.Links.Count; i++)
                        {

                            if (entry.Links[i].Rel == "edit")
                            {
                                string commentUriStart = Regex.Replace(blogPostUri.ToString(), "/posts/default", "");
                                string selfLink = entry.Links[i].HRef.ToString();
                                string entryId = Regex.Replace(selfLink, blogPostUri.ToString() + "/", "");
                                // Build the comment URI from the blog id in and the entry id.
                                commentPostUri = new Uri(commentUriStart + "/" + entryId + "/comments/default");
                                Console.WriteLine("  Your new comments will be sent to " + commentPostUri.ToString());
                                return commentPostUri;
                            }
                        }
                    }
                }
            }

            return commentPostUri;
        }

        public AtomEntry PostNewComment(Service service, Uri commentPostUri)
        {
            Console.WriteLine("\nCommenting on a blog post");
            AtomEntry postedComment = null;
            if (commentPostUri != null)
            {
                // Add a comment.
                AtomEntry comment;
                comment = new AtomEntry();
                comment.Title.Text = "This is my first comment";
                comment.Content.Content = "This is my first comment";
                comment.Authors.Add(new AtomPerson());
                comment.Authors[0].Name = "Blog Author Name";
                postedComment = service.Insert(commentPostUri, comment);
                Console.WriteLine("  Result's title: " + postedComment.Title.Text);
            }
            return postedComment;
        }

        public void ListEntryComments(Service service, Uri commentUri)
        {
            if (commentUri != null)
            {
                Console.WriteLine("\nRetrieving all blog post comments");
                // Retrieve all comments on a blog entry
                FeedQuery query = new FeedQuery();
                Console.WriteLine("  Query URI: " + commentUri.ToString());
                query.Uri = commentUri;
                AtomFeed feed = service.Query(query);
                foreach (AtomEntry entry in feed.Entries)
                {
                    Console.WriteLine("  Comment Title: " + entry.Title.Text);
                }
            }
        }

        public void DeleteComment(AtomEntry commentEntry)
        {
            Console.WriteLine("\nDeleting the comment");
            if (commentEntry != null)
            {
                // Delete the comment.
                Console.WriteLine("  Press enter to delete the new comment post");
                Console.ReadLine();
                commentEntry.Delete();
            }
        }

        ///** Display the titles for all entries in the previously selected blog. */
        //public void FindBlogEntry(Service service, Uri blogUri, string postTitle)
        //{
        //    if (blogUri != null)
        //    {
        //        // Retrieve all posts in a blog
        //        FeedQuery query = new FeedQuery();
        //        //Console.WriteLine("  Query URI: " + blogUri.ToString());
        //        query.Uri = blogUri;
        //        AtomFeed feed = service.Query(query);
        //        foreach (AtomEntry entry in feed.Entries)
        //        {
        //            if (entry.Title.Text.ToLower().Contains(postTitle.ToLower()))
        //        }
        //    }
        //}
                //Console.WriteLine("\nRetrieving all blog posts");

        ///// <summary>
        ///// Test post a new entry
        ///// </summary>
        ///// <param name="objPostCrawler"></param>
        ///// <param name="args"></param>
        //public void AddNewPost(Service service, Uri blogPostUri, PostCrawler objPostCrawler)
        //{
        //    //WapDataClassesDataContext dc = new WapDataClassesDataContext();
        //    //PostCrawler objPostCrawler = dc.PostCrawlers.Single(p => p.postCrawlerId == postCrawlerId);
        //    //Nếu chưa thì thực hiện post mới
        //    AtomEntry createdEntry = PostNewEntry(service, blogPostUri, objPostCrawler);
          
        //}
    }
}
